<template>
  <!-- <vue-ztree :list.sync="options" :is-open="true" :is-check="true"></vue-ztree> -->
  <Treeselect
    v-model="checked"
    :multiple="setting.multiple"
    :options="options"
    noOptionsText="暂无选项"
    noChildrenText="暂无成员"
    placeholder="请选择"
    :closeOnSelect="true"
    style="width:100%"
    :disableBranchNodes="true"
  />
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import { getOrganUserAll } from "api/seting/user";
export default {
  components: {
    Treeselect
  },
  props:["setting"],
  data() {
    return {
      checked: [],
      options: [], // 组织架构成员
      members: [],
      organs: []
    };
  },
  created() {
    this.getMembers();
  },
  methods: {
    //获取员工
    getMembers() {
      getOrganUserAll().then(res => {
        this.organs = res.data.organs;
        this.members = res.data.organUsers;
        this.options = this.generateOptions(res.data.organs);
        console.log(this.options, "xxxoptions");
      });
    },
    generateOptions(datas) {
      //生成树状数据
      var result = [];
      for (let data of datas) {
        if (data.parentCode == "0") {
          //判断是否为顶层节点
          let parent = {
            label: data.organName,
            id: data.id,
          };
          parent.children = this.getUsers(data.id, this.members);
          parent.children.push(...this.getchilds(data.organCode, datas)); //获取子节点
          result.push(parent);
        }
      }
      return result;
    },
    getchilds(code, array) {
      let childs = new Array();
      for (let arr of array) {
        //循环获取子节点
        if (arr.parentCode == code) {
          let child = {
            id: arr.organCode,
            label: arr.organName,
          };
          child.children = this.getUsers(arr.id, this.members);
          childs.push(child);
        }
      }
      for (let child of childs) {
        //获取子节点的子节点
        let childscopy = this.getchilds(child.id, array); //递归获取子节点
        if (childscopy.length > 0) {
          child.children.push(...childscopy);
        }
      }
      return childs;
    },
    // 获取成员
    getUsers(id, members) {
      let users = new Array();
      for (let member of members) {
        //循环获取子节点
        if (member.organId == id) {
          users.push(
            ...member.users.map(o => {
              return {
                id: o.id,
                label: o.realName
              };
            })
          );
        }
      }
      return users;
    }
  }
};
</script>

<style lang='scss' scoped>
</style>
